//
//  UIButton+ImageAndTitleAlignment.m
//  Wutuojia
//
//  Created by anlun on 15/7/16.
//  Copyright (c) 2015年 anLun. All rights reserved.
//

#import "UIButton+ImageAndTitleAlignment.h"
#import "UIView+Frame.h"
#import "HSComAction.h"

@implementation UIButton (ImageAndTitleAlignment)
- (void)setAlignment{
    //    [self setBackgroundColor:[UIColor grayColor]];
    //    [self.titleLabel setBackgroundColor:[UIColor greenColor]];
    
    [self setImageEdgeInsets:UIEdgeInsetsZero];
    [self setTitleEdgeInsets:UIEdgeInsetsZero];
    
    float width=0;
    
    [self setImageEdgeInsets:UIEdgeInsetsMake(2,
                                              (self.width-self.imageView.width)/2.0f,
                                              self.height-self.imageView.height,
                                              (self.width-self.imageView.width)/2.0f)];
    if (self.selected) {
        [self.titleLabel setWidth:[HSComAction getSizeByStr:[self titleForState:UIControlStateSelected]
                                                    andFont:self.titleLabel.font].width];
        width=[HSComAction getSizeByStr:[self titleForState:UIControlStateSelected]
                                andFont:self.titleLabel.font].width;
        
    }
    else{
        [self.titleLabel setWidth:[HSComAction getSizeByStr:[self titleForState:UIControlStateNormal]
                                                    andFont:self.titleLabel.font].width];
        width=[HSComAction getSizeByStr:[self titleForState:UIControlStateNormal]
                                andFont:self.titleLabel.font].width;
    }
    
    [self setTitleEdgeInsets:UIEdgeInsetsMake(self.imageView.height,
                                              (self.width-width)/2.0f-self.imageView.width,
                                              0,
                                              (self.width-width)/2.0f-self.imageView.width)];
}
@end
